var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = 'yyyy-MM-dd hh:mm:ss';
function padding(s, len) {
    var len = len - (s + '').length;
    for (var i = 0; i < len; i++) { s = '0' + s; }
    return s;
};

/**
 * 格式化时间
 * 
 * @param {String} str
 * @returns 格式化后的时间
 */
export function formatDate(date, pattern) {
    if (!date) return ''
    date = new Date(date)
    pattern = pattern || DEFAULT_PATTERN;
    return pattern.replace(SIGN_REGEXP, function ($0) {
        switch ($0.charAt(0)) {
            case 'y': return padding(date.getFullYear(), $0.length);
            case 'M': return padding(date.getMonth() + 1, $0.length);
            case 'd': return padding(date.getDate(), $0.length);
            case 'w': return date.getDay() + 1;
            case 'h': return padding(date.getHours(), $0.length);
            case 'm': return padding(date.getMinutes(), $0.length);
            case 's': return padding(date.getSeconds(), $0.length);
        }
    });
}

/**
 * 返回周几
*/
export function getDay(date) {
    var a = new Array("日", "一", "二", "三", "四", "五", "六");
    var week = new Date(date).getDay();
    return "周" + a[week];
}

export function parseDate(dateString, pattern) {
    var matchs1 = pattern.match(SIGN_REGEXP);
    var matchs2 = dateString.match(/(\d)+/g);
    if (matchs1.length == matchs2.length) {
        var _date = new Date(1970, 0, 1);
        for (var i = 0; i < matchs1.length; i++) {
            var _int = parseInt(matchs2[i]);
            var sign = matchs1[i];
            switch (sign.charAt(0)) {
                case 'y': _date.setFullYear(_int); break;
                case 'M': _date.setMonth(_int - 1); break;
                case 'd': _date.setDate(_int); break;
                case 'h': _date.setHours(_int); break;
                case 'm': _date.setMinutes(_int); break;
                case 's': _date.setSeconds(_int); break;
            }
        }
        return _date;
    }
    return null;
}
/**
 * 金额
 * @param {Number} s 
 * @param {Number} n 小数点位数
 * @returns 金额
 */
export function amount(s, n) {
    if (!s) {
        return;
    }
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + '').replace(/[^\d/.-]/g, '')).toFixed(n) + '';
    let l = s.split('.')[0].split('').reverse();
    let r = s.split('.')[1];
    let t = '';
    for (let i = 0; i < l.length; i++) {
        let a = i + 1;
        t += l[i] + (a % 3 === 0 && a !== l.length ? ',' : '');
    };
    return t.split('').reverse().join('') + '.' + r;
}

//过滤类型
let TYPE = {};
inFilter();
let filterType = {
    riskType(id) {
        return TYPE.riskType[id];
    },
    // 风险类別
    riskCategory(id) {
        return TYPE.riskCategory[id];
    },
    // 风险等级
    riskLevel(id) {
        return TYPE.riskLevel[id];
    },
    // 产品类型类别
    productType(id) {
        return TYPE.productType[id];
    },
    // 来源渠道
    sourceType(id) {
        return TYPE.sourceType[id];
    },
    // 机构简称
    orgType(id) {
        return TYPE.orgType[id];
    },
    // 案件类型
    caseType(id) {
        return TYPE.caseType[id];
    },
    // 案件状态
    caseStatus(id) {
        return TYPE.caseStatus[id];
    },
    // 线索状态
    clueStatus(id) {
        return TYPE.clueStatus[id];
    }
}

function inFilter() {
    let dicts = JSON.parse(localStorage.getItem('DICT_DF_ALL'));
    let typeArr = {
        // 风险类型
        riskType: dicts['risk_type'] || [],
        // 风险类別
        riskCategory: dicts['risk_category'] || [],
        // 风险等级
        riskLevel: dicts['risk_level'] || [],
        // 产品类型类别 
        productType: dicts['product_type'] || [],
        // 案件/线索来源渠道
        sourceType: dicts['source_type'] || [],
        // 机构简称
        orgType: dicts['org_type'] || [],
        // 案件类型
        caseType: dicts['case_type'] || [],
        // 案件状态
        caseStatus: dicts['case_status'] || [],
        // 线索状态
        clueStatus: dicts['clue_status'] || [],
    }

    // 遍历成字典
    for (let key in typeArr) {
        TYPE[key] = {};
        for (let i = 0, len = typeArr[key].length; i < len; i++) {
            TYPE[key][typeArr[key][i].value] = typeArr[key][i].name;
        }
    }
}

export default {
    filters: {
        filterType,
        getDay,
        formatDate,
        parseDate,
        amount,
    }
}